package com.glsc.ngateway.common.base.repo.mysql.gateway.ldap;

import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.LdapEhrComp;
import com.glsc.ngateway.common.api.ldap.dto.LdapEhrCompSearchDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lium
 * @date 2022/5/9
 */
public interface LdapEhrCompRepo extends JpaRepository<LdapEhrComp, Integer>, JpaSpecificationExecutor<LdapEhrComp> {

    @Transactional
    @Modifying
    @Query(value = "truncate table t_ldap_ehr_comp", nativeQuery = true)
    Integer truncateTableEhrCompy();

    @Query(value = "select * from t_ldap_ehr_comp t " +
            "where 1=1 " +
            "and (coalesce(:#{#param.oaCompIdList}, null) is null or t.oa_comp_id in (:#{#param.oaCompIdList})) " +
            "and (:#{#param.oaCompId} is null or t.oa_comp_id = :#{#param.oaCompId}) " +
            "", nativeQuery = true)
    List<LdapEhrComp> query(@Param("param") LdapEhrCompSearchDto param);

    List<LdapEhrComp> findAllByOaCompId(Integer oaCompId);
}